package com.guanjia.web;

import com.guanjia.entity.CrowdFunding;
import com.guanjia.entity.User;
import com.guanjia.mapper.UserMapper;
import com.guanjia.service.CrowdFundingService;
import com.guanjia.service.impl.CrowdFundingServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 交易控制器
 */
@Controller
@RequestMapping("/deal")
public class DealController {
    @Autowired
    private CrowdFundingService crowdFundingService;

    @Autowired
    private UserMapper userMapper;

    /**
     * 众筹交易
     */
    @ResponseBody
    @RequestMapping("/zhongChou")
    public String zhongChouDeal(@RequestParam("money") String m, @RequestParam("crowdId") String crowdId, HttpSession session){
        Integer money=0;
        try {
           money =Integer.parseInt(m);

        }catch (Exception e){
            return "请输入正确的整数";
        }
        if (money<=0){
            return "请输入正确的整数";
        }


            User user =(User) session.getAttribute("user");
               if(user==null){
                   return "login";

               }
               User u=userMapper.findUserById(user.getId());
               if (u.getBalance()<Double.valueOf(money)){
                   return "您的账户余额不足QAQ";
               }
        CrowdFunding crowdFunding=crowdFundingService.findCrowdById(Integer.parseInt(crowdId));
               if (crowdFunding.getUserId().equals(user.getId())){
                   return "不能给自己投资哦！";
               }
            crowdFundingService.insertDealAndBussiness(m,crowdId,u.getId(),crowdFunding.getUserId());

             System.out.println(user);
             return "ok";
    }

}
